﻿using System;
using Sigil.Impl;

namespace Sigil
{
	/// <summary>
	/// Represents a Label in a CIL stream, and thus a Leave and Branch target.
	///
	/// To create a Label call DefineLabel().
	///
	/// Before creating a delegate, all Labels must be marked.  To mark a label, call MarkLabel().
	/// </summary>
	public class Label : IOwned
	{
		/// <summary>
		/// The name of this Label.
		///
		/// If one is omitted during creation a random one is created instead.
		///
		/// Names are purely for debugging aid, and will not appear in the generated delegate.
		/// </summary>
		public String Name { get; private set; }

		internal DefineLabelDelegate LabelDel { get; private set; }

		private Object _Owner;

		Object IOwned.Owner { get { return _Owner; } }

		internal Label(Object owner, DefineLabelDelegate label, String name)
		{
			_Owner = owner;
			Name = name;
			LabelDel = label;
		}

		internal void SetOwner(Object owner)
		{
			if (_Owner != null && owner != null) throw new Exception("Cannot set ownership of an owner Label");

			_Owner = owner;
		}

		/// <summary>
		/// Equivalent to Name.
		/// </summary>
		public override String ToString()
		{
			return Name;
		}
	}
}